package com.cloud;

import com.cloud.model.entity.ASTNode;
import com.cloud.model.entity.ExpressionParser;
import com.cloud.model.req.ComplexFilter;
import com.cloud.utils.RuleUtils;

import java.util.List;

public class Main {
    public static void main(String[] args) {
        // 输入的逻辑表达式
        String input = "(param1 == '02-滚筒洗衣机' and param2 == '03-波轮洗衣机') and (param3 == '12-洗碗机' or (param4 == '18-干衣机' and param5 == '19-吹风机')) or param6 == '00-通用类'";

        // 步骤 1：生成 AST
        ExpressionParser parser = new ExpressionParser(input);
        ASTNode astRoot = parser.parse();

        // 步骤 2：将 AST 转换为 ComplexFilter
        ComplexFilter complexFilter = RuleUtils.convertToComplexFilter(astRoot);

        // 输出结果
        System.out.println("生成的 ComplexFilter 树:");
        printComplexFilter(complexFilter, 0);
        String sql = RuleUtils.buildSql(complexFilter);
        System.out.println("SQL 语句:");
        System.out.println(sql);
    }

    // 递归打印 ComplexFilter 树
    private static void printComplexFilter(ComplexFilter filter, int indent) {
        for (int i = 0; i < indent; i++) {
            System.out.print("  ");
        }

        if (filter.getJoiner() != null) {
            // 打印逻辑节点
            System.out.println("JOINER: " + filter.getJoiner());
            List<ComplexFilter> subFilters = filter.getFilters();
            if (subFilters != null) {
                for (ComplexFilter subFilter : subFilters) {
                    printComplexFilter(subFilter, indent + 1);
                }
            }
        } else {
            // 打印叶子节点
            System.out.println("CONDITION: " +
                    filter.getProperties() + " " +
                    filter.getOps() + " " +
                    filter.getFilterValue());
        }
    }
}